package com.gitee.taotaojs.mybootsuper.controller;

import com.gitee.taotaojs.mybootsuper.bean.AppBack;
import com.gitee.taotaojs.mybootsuper.bean.BaseBean;
import com.gitee.taotaojs.mybootsuper.bean.BatchDTOBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

/**
 * 通用Controller
 * @author TaoTaojs
 * @date 2020年03月20日15:49:46
 * <p>Description</p>
 * 基本Contorller方法，可直接使用URL调用（慎重继承）
 * 关联类:
 * 参考链接：
 */
public class MyBatchController<T extends BaseBean> extends MyComController<T> {


    @ApiOperation(
            value="批量新增对象", httpMethod = POST,
            notes = ""
    )
    @PostMapping("/batchAdd")
    @ResponseBody
    public AppBack<List<T>> batchAdd(@Valid @RequestBody BatchDTOBean<T> entity, T baseBean){
        beanService.batchInsertObject(entity.getBaseBeanList(), baseBean);
        return new AppBack<>(entity.getBaseBeanList());
    }

    @ApiOperation(
            value="批量更新对象", httpMethod = PUT,
            notes = ""
    )
    @PutMapping("/batchUpdate")
    @ResponseBody
    public AppBack<List<T>> batchUpdate(@Valid @RequestBody BatchDTOBean<T> entity, T baseBean, boolean upNull){
        beanService.batchUpdateObject(entity.getBaseBeanList(), baseBean);
        return new AppBack<>(entity.getBaseBeanList());
    }

    @ApiOperation(
            value="批量假删除对象", httpMethod = PUT,
            notes = ""
    )
    @PutMapping("/batchUpdateByDelete")
    @ResponseBody
    public AppBack<List<T>> batchUpdateByDeleteObject(@Valid @RequestBody BatchDTOBean<T> entity, T baseBean){
        beanService.batchUpdateByDeleteObject(entity.getBaseBeanList(), baseBean);
        return new AppBack<>(entity.getBaseBeanList());
    }


    @ApiOperation(
            value="批量删除对象", httpMethod = DELETE,
            notes = ""
    )
    @DeleteMapping("/batchDelete")
    @ResponseBody
    public AppBack<List<T>> batchDelete(@Valid @RequestBody BatchDTOBean<T> entity, T baseBean){
        beanService.batchDeleteObject(entity.getBaseBeanList(), baseBean);
        return new AppBack<>(entity.getBaseBeanList());
    }


}
